import pingYing from '/pinying.js';
import http from '../apis/api.js';

function cityList(cb){
  return new Promise((resole, reject) => {
    http.getTencentMapDistrict().then(res => {
      if(res.code==0){
        let list = [
          { "letter": "A", "data": [] },
          { "letter": "B", "data": [] },
          { "letter": "C", "data": [] },
          { "letter": "D", "data": [] },
          { "letter": "E", "data": [] },
          { "letter": "F", "data": [] },
          { "letter": "G", "data": [] },
          { "letter": "H", "data": [] },
          { "letter": "I", "data": [] },
          { "letter": "J", "data": [] },
          { "letter": "K", "data": [] },
          { "letter": "L", "data": [] },
          { "letter": "M", "data": [] },
          { "letter": "N", "data": [] },
          { "letter": "O", "data": [] },
          { "letter": "P", "data": [] },
          { "letter": "Q", "data": [] },
          { "letter": "R", "data": [] },
          { "letter": "S", "data": [] },
          { "letter": "T", "data": [] },
          { "letter": "U", "data": [] },
          { "letter": "V", "data": [] },
          { "letter": "W", "data": [] },
          { "letter": "X", "data": [] },
          { "letter": "Y", "data": [] },
          { "letter": "Z", "data": [] }]
        let district = [];
        res.data.province.forEach(el => {
          if (el.city.length == 1) {
            district.push({
              fullname: el.fullname.replace('市',''),
              id: el.id,
              lat: el.lat,
              lng: el.lng,
              name: el.name,
              camel: pingYing.getCamelChars(el.fullname)
            })
          } else {
            el.city.forEach(item => {
              district.push({
                fullname: item.fullname.replace('市', ''),
                id: item.id,
                lat: item.lat,
                lng: item.lng,
                name: item.name,
                camel: pingYing.getCamelChars(item.fullname)
              })
            })
          }
        })
        list.forEach(el => {
          district.forEach(di => {
            if (di.camel.indexOf(el.letter) == 0) {
              el.data.push(di);
            }
          })
        })
        resole(list);
      }else{
        reject(res);
      } 
    })
  })

}
 
module.exports = cityList;